package com.jwork.progress.params;

import com.jwork.progress.enums.RespCodeEnums;
import lombok.Data;
import org.springframework.util.ObjectUtils;

@Data
public class CommonResponse<T> {
    private int code;

    private String msg;

    private T data;

    /**
     * 全参方法
     *
     * @param code
     * @param msg
     * @param data
     * @param <T>
     * @return
     */
    private static <T> CommonResponse<T> response(int code, String msg, T data) {
        CommonResponse<T> respInfo = new CommonResponse<>();
        respInfo.setCode(code);
        respInfo.setMsg(msg);
        respInfo.setData(data);

        return respInfo;
    }

    /**
     * 缺失data方法
     *
     * @param code
     * @param msg
     * @param <T>
     * @return
     */
    private static <T> CommonResponse<T> response(int code, String msg) {
        CommonResponse<T> respInfo = new CommonResponse<>();
        respInfo.setCode(code);
        respInfo.setMsg(msg);

        return respInfo;
    }

    public static <T> CommonResponse<T> success(String msg) {
        msg = ObjectUtils.isEmpty(msg) ? RespCodeEnums.SUCCESS.getMsg() : msg;
        return response(RespCodeEnums.SUCCESS.getCode(), msg);
    }

    public static <T> CommonResponse<T> success(T data) {
        return response(RespCodeEnums.SUCCESS.getCode(), RespCodeEnums.SUCCESS.getMsg(), data);
    }

    public static <T> CommonResponse<T> fail(String msg) {
        msg = ObjectUtils.isEmpty(msg) ? RespCodeEnums.FAIL.getMsg() : msg;
        return response(RespCodeEnums.FAIL.getCode(), msg);
    }

    public static <T> CommonResponse<T> fail(int code, String msg) {
        return response(code, msg);
    }

    public static <T> CommonResponse<T> fail(int code, String msg, T data) {
        return response(code, msg, data);
    }
}
